package com.vnp.vchat.version2;

import org.json.JSONException;
import org.json.JSONObject;

import com.vnp.vchat.R;
import com.vnp.vchat.httpclient.HttpClientHelper;
import com.vnp.vchat.httpclient.MyJsonHttpResponseHandler;
import com.vnp.vchat.util.KeyboardHelper;
import com.vnp.vchat.util.Utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class DangkiNhapCodeActivity extends Activity implements OnClickListener{
	TextView title, btnFunction2, titleNhapMaCode;
	
	Button btnTieptuc;	
	ImageButton btnBack;
	
	EditText edtMacode;
	
	String accID;
	String phone;
	ProgressBar progressBar;
	
	Dialog dConfirm;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dangki_nhap_code);
		
		phone = getIntent().getStringExtra("phone");
		
		title = (TextView) findViewById(R.id.title);
		btnFunction2 = (TextView) findViewById(R.id.btn_funtion2);
		btnFunction2.setVisibility(View.GONE);
		btnBack = (ImageButton) findViewById(R.id.btn_function1);
		btnBack.setVisibility(View.VISIBLE);
		progressBar = (ProgressBar) findViewById(R.id.progressBar1);
		
		btnTieptuc = (Button) findViewById(R.id.btn_tieptuc);
		
		edtMacode = (EditText) findViewById(R.id.edt_ma_code);
		titleNhapMaCode = (TextView) findViewById(R.id.txt_nhap_code);
		
		init();
		
		setFont();
		
		sendPhone();
	}
	
	void setFont(){
		titleNhapMaCode.setTypeface(Utils.getTypefaceRegular(DangkiNhapCodeActivity.this));
		title.setTypeface(Utils.getTypefaceBold(DangkiNhapCodeActivity.this));
		edtMacode.setTypeface(Utils.getTypefaceRegular(DangkiNhapCodeActivity.this));
		btnTieptuc.setTypeface(Utils.getTypefaceMedium(DangkiNhapCodeActivity.this));
	}
	
	void init(){
		title.setText("Đăng ký");
		btnBack.setOnClickListener(this);
		btnTieptuc.setOnClickListener(this);
	}
	
	void sendPhone(){
		
		HttpClientHelper httpClientHelper = new HttpClientHelper(new MyJsonHttpResponseHandler(){

			@Override
			public void onSuccess(int statusCode, JSONObject re) {
				if (statusCode == 200){
					btnTieptuc.setText("Đồng ý");
					btnTieptuc.setClickable(true);
					Log.i("NHAPPHONE", "TRA VE: "+re.toString());
					try {
						accID = String.valueOf(re.getInt("acc_id"));
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}

			@Override
			public void onFailure(int statusCode, Throwable error) {
				if (statusCode == 400){
					Toast.makeText(DangkiNhapCodeActivity.this, "Số điện thoại không đúng hoặc yêu cầu quá nhiều với số điện thoại này.", Toast.LENGTH_SHORT).show();
				} else if (statusCode == 403) {
					error.printStackTrace();
					Toast.makeText(DangkiNhapCodeActivity.this, "Số điện thoại đã tồn tại trên hệ thống.", Toast.LENGTH_SHORT).show();
				}
				
			}

		});
		
		httpClientHelper.sendPhone(phone);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_function1:
//			closeActivity();
			showAlert();
			break;
		case R.id.btn_tieptuc:
			if (edtMacode.getText().toString().equals("")){
				Toast.makeText(DangkiNhapCodeActivity.this, "Bạn chưa nhập mã code.", Toast.LENGTH_SHORT).show();
			} else {
				sendActiveCode();
//				capnhatThongtin();
			}
			break;
		case R.id.btn_edit:
			dConfirm.dismiss();
			break;
			
		case R.id.btn_ok:
			dConfirm.dismiss();
			closeActivity();
			break;
		default:
			break;
		}
	}
	
	void sendActiveCode(){
		progressBar.setVisibility(View.VISIBLE);
		edtMacode.setEnabled(false);
		btnTieptuc.setClickable(false);
		btnTieptuc.setText("Đang xác thực...");
		
		HttpClientHelper httpClientHelper = new HttpClientHelper(new MyJsonHttpResponseHandler(){

			@Override
			public void onFailure(int statusCode, Throwable error) {
				progressBar.setVisibility(View.GONE);
				edtMacode.setEnabled(true);
				btnTieptuc.setClickable(true);
				btnTieptuc.setText("Đồng ý");
				
				
				if (statusCode == 400){
					Toast.makeText(DangkiNhapCodeActivity.this, "Không tìm thấy tài khoản", Toast.LENGTH_SHORT).show();
				} else if ( statusCode == 404) {
					Toast.makeText(DangkiNhapCodeActivity.this, "Sai mã kích hoạt", Toast.LENGTH_SHORT).show();
				}
				
			}

			@Override
			public void onSuccess(int statusCode, JSONObject re) {
				edtMacode.setEnabled(true);
				btnTieptuc.setClickable(true);
				btnTieptuc.setText("Đồng ý");
				progressBar.setVisibility(View.GONE);
				if (statusCode == 200){
					capnhatThongtin();
				}
			}
		});
		
		httpClientHelper.sendCode(accID, edtMacode.getText().toString());
	}
	
	
	void capnhatThongtin(){
		KeyboardHelper.hideSoftKeyboard(DangkiNhapCodeActivity.this);
		Intent t = new Intent(DangkiNhapCodeActivity.this, CapNhatThongTinActivity.class);
		t.putExtra("acc_id", accID);
		t.putExtra("phone", phone);
		startActivity(t);
		overridePendingTransition(R.anim.trans_left_in, R.anim.over_main_left);
	}
	
	@Override
	public void onBackPressed() {
		closeActivity();
	}
	
	void closeActivity(){
		finish();
		overridePendingTransition(R.anim.over_main_right, R.anim.trans_right_out);
	}
	
	void showAlert(){
		dConfirm = new Dialog(this);
		dConfirm.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dConfirm.setContentView(R.layout.dialog_confirm_send_phone);
		
		TextView txtTitle = (TextView) dConfirm.findViewById(R.id.title_confirm);
		TextView txtThongbao = (TextView) dConfirm.findViewById(R.id.str_send_phone);
		
		Button btnChinhsua = (Button) dConfirm.findViewById(R.id.btn_edit);
		Button btnOk = (Button) dConfirm.findViewById(R.id.btn_ok);
		dConfirm.show();
		dConfirm.getWindow().setBackgroundDrawable(new ColorDrawable(0));
		dConfirm.setCanceledOnTouchOutside(true);
		dConfirm.getWindow().setGravity(Gravity.CENTER);
		dConfirm.show();
		
		txtTitle.setTypeface(Utils.getTypefaceBold(DangkiNhapCodeActivity.this));
		txtThongbao.setTypeface(Utils.getTypefaceMedium(DangkiNhapCodeActivity.this));
		btnChinhsua.setTypeface(Utils.getTypefaceRegular(DangkiNhapCodeActivity.this));
		btnOk.setTypeface(Utils.getTypefaceRegular(DangkiNhapCodeActivity.this));
		// setonclick
		btnChinhsua.setOnClickListener(this);
		btnOk.setOnClickListener(this);
	}
	
}
